package com.xiaxiang.xiawan;

import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.dg.acb.utils.SharedPreferencesCtrl;
import com.dg.acb.utils.configdata.DGConfigData;
import com.dg.acb.utils.configdata.XXUserInfoData;
import com.xiaxiang.xiawan.Util.FileUtil;
import com.xiaxiang.xiawan.login.UserInfoBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2017-10-29.
 */
//com.xiaxiang.xiawan.XXMainApplication
public class XXMainApplication extends Application {
    private static XXMainApplication sInstance;
    private String mIsLogin = "";
    private DGConfigData mConfigData;
    private final static String TRUE = "true";
    private final static String FALSE = "false";
    private final static String IS_LOGIN_KEY = "isLogin";
    private XXUserInfoData mUserInfo = null;
    public String mToken = "";
    public String mUserName = "未登录";
    public String mIconUrl = "";
    private String mMachineId = "sddd";
    private SharedPreferencesCtrl mSharedPreferencesCtrl;
    private final static String INFO_NAME = "xxUserInfo";
    private final static String INFO_KEY= "userInfo";
    public void onCreate(){
        super.onCreate();
        sInstance = this;
        mSharedPreferencesCtrl = new SharedPreferencesCtrl(this,INFO_NAME);
        mConfigData = new DGConfigData(this);
        new Thread(){
            public void run(){
                super.run();
                TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                mMachineId = tm.getDeviceId();
                Log.e("me"," meid is "+mMachineId);
                initUserInfo();
                String path  = "/mnt/emmc";
                File file = new File(path);
                if(!file.exists()){
                    File filePath = getExternalFilesDir(null);
                    if(filePath != null){
                        path = filePath.getAbsolutePath();
                    }
                }
                FileUtil.SDPATH = path;
                String downloadPath = FileUtil.getUnity3dDownloadFolder();
                File file1 = new File(downloadPath);
                if(!file1.exists()){
                    file1.mkdirs();
                }
                String sdcardPath = XXConfigData.getCachePath();
                File file2 = new File(sdcardPath);
                if(!file2.exists()){
                    file2.mkdirs();
                }
            }
        }.start();

    }

    public void initUserInfo() {
        mSharedPreferencesCtrl = new SharedPreferencesCtrl(this,INFO_NAME);//确保能够跨进程更新数据
        String infoJsonStr =  mSharedPreferencesCtrl.getString(INFO_KEY,"");
        Log.e("info"," jsonStr is "+infoJsonStr);
        if(infoJsonStr.length() != 0){
            try {
                mUserInfo = new XXUserInfoData(infoJsonStr);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }else{
            mUserInfo = null;
        }
    }

    public static XXMainApplication getInstance() {
        return sInstance;
    }

    public void setIsLogin(boolean is){
        if(is){
            mIsLogin = TRUE ;
        }else{
            mIsLogin = FALSE;
        }
        mConfigData.updateConf(IS_LOGIN_KEY,mIsLogin);
    }

    public boolean getIsLogin(){
     //   initUserInfo();
        if(mUserInfo == null){
            return false;
        }
        return mUserInfo.mIsLogin;
    }

    public void setToken(String token){
        if(mToken == null || mToken.length() == 0){
            mConfigData.insertToken(token);
        }else{
            mConfigData.updateToken(token);
        }
        mToken = token;
    }

    public String getToken(){
        if(mUserInfo == null){
            return "";
        }
        return mUserInfo.mToken;
    }

    public String getUserName(){
  //      initUserInfo();
        if(mUserInfo == null){
            return "";
        }
        return mUserInfo.mUser;
    }


    public String getmIconUrl(){
       // initUserInfo();
        if (mUserInfo != null) {
            mIconUrl = mUserInfo.mHeadUrl;
        }else {
            mIconUrl = "";
        }
        return mIconUrl == null ? "" : mIconUrl;
    }

    public void setUserInfo(XXUserInfoData userInfo) {
        if (userInfo == null) {
            return;
        }
        /*
        if (mUserInfo == null) {
            mConfigData.insertUserInfoData(this, userInfo);
        } else {
            // mConfigData.up
            mConfigData.updateUserInfo(this, userInfo.mUser, userInfo);
        }*/
        mSharedPreferencesCtrl.put(INFO_KEY,userInfo.toString());
        mUserInfo = userInfo;
    }

    public void setUserInfo(String jsonStr) {
        if (jsonStr == null || jsonStr.length() == 0) {
            return;
        }
        mSharedPreferencesCtrl.put(INFO_KEY,jsonStr);
        try {
            mUserInfo = new XXUserInfoData(jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public String getMachineId(){
        return mMachineId;
    }

    public void logout(){
        mSharedPreferencesCtrl.clear();
        mUserInfo = null;
    }
}
